using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;

namespace SPALM.SharePointSoftwareFactory.Installer
{
  /// <summary>
  /// Registers the software factory for project types c#, workflows
  /// </summary>
  public class VSTemplatesRegister
  {
    private static string projectType1 = "{14822709-B5A1-4724-98CA-57A101D1B079}"; //workflow
    private static string projectType2 = "{349C5853-65DF-11DA-9384-00065B846F21}";
    private static string projectType3 = "{593B0543-81F6-4436-BA1E-4747859CAAE2}";

    public static void Register(string packageGuid, string templatesDir, string packageName)
    {
      string specialPackageGuid = GetSpecialPackageGuid(packageGuid);

      RegisterGaxPackage(specialPackageGuid, templatesDir, projectType1, packageName);
      RegisterGaxPackage(specialPackageGuid, templatesDir, projectType2, packageName);
      RegisterGaxPackage(specialPackageGuid, templatesDir, projectType3, packageName);
    }

    public static void Unregister(string packageGuid)
    {
      string specialPackageGuid = GetSpecialPackageGuid(packageGuid); // = "{14822709-B5A1-4724-98CA-57A101D1B079}";

      UnregisterGaxPackage(specialPackageGuid, projectType1);
      UnregisterGaxPackage(specialPackageGuid, projectType2);
      UnregisterGaxPackage(specialPackageGuid, projectType3);
    }

    private static string GetSpecialPackageGuid(string originalPackageGuid)
    {
      originalPackageGuid = originalPackageGuid.Substring(0, originalPackageGuid.LastIndexOf("-"));
      originalPackageGuid = originalPackageGuid + "-SPSF20101120}";
      return originalPackageGuid;
    }

    private static void UnregisterGaxPackage(string packageGuid, string projectType)
    {
      try
      {
        RegistryKey startupKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\\Microsoft\\VisualStudio\\9.0\\Projects\\" + projectType + "\\AddItemTemplates\\TemplateDirs\\", true);

        RegistryKey gaxKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\\Microsoft\\VisualStudio\\9.0\\Projects\\" + projectType + "\\AddItemTemplates\\TemplateDirs\\" + packageGuid + "\\", false);
        if (gaxKey != null)
        {
          startupKey.DeleteSubKeyTree(packageGuid);
        }
      }
      catch 
      {

      }
    }

    private static void RegisterGaxPackage(string packageGuid, string templatesDir, string projectType, string packageName)
    {
      //open the template key for Workflows
      try
      {
        RegistryKey startupKey = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\\Microsoft\\VisualStudio\\9.0\\Projects\\" + projectType + "\\AddItemTemplates\\TemplateDirs\\");

        //is there already our guidance package?
        RegistryKey gaxkey = null;
        try
        {
          gaxkey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\VisualStudio\\9.0\\Projects\\" + projectType + "\\AddItemTemplates\\TemplateDirs\\" + packageGuid + "\\/1", false);
        }
        catch (Exception)
        {
        }
        if (gaxkey == null)
        {
          //key missing, create now
          RegistryKey gaxkey1 = startupKey.CreateSubKey(packageGuid);
          RegistryKey gaxkey2 = gaxkey1.CreateSubKey("/1");

          gaxkey2.SetValue(string.Empty, packageName);
          gaxkey2.SetValue("SortPriority", 10, RegistryValueKind.DWord);
          gaxkey2.SetValue("TemplatesDir", templatesDir);
        }
      }
      catch 
      {
      }
    }
  }
}
